package com.freshcity.upload
{
	import com.freshcity.upload.ui.RectBox;
	import com.freshcity.upload.ui.TipB;
	
	import fl.controls.Button;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.geom.Matrix;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.system.Security;
	import flash.system.SecurityPanel;
	import flash.utils.Timer;
	
	public class CameraComp extends Sprite
	{
		public function CameraComp(parent:CutView)
		{
			super();
			this.init(parent);
		}
		
		
		private function showNowScene(arg1:TimerEvent):void
		{
			this._t100.removeEventListener(TimerEvent.TIMER, this.showNowScene);
			this._t100.reset();
			this.getAvatarBMD();
			this._t600 = this._t600 || new Timer(600);
			this._t600.addEventListener(TimerEvent.TIMER, this.changeScene);
			this._t600.start();
			return;
		}
		
		private function getAvatarBMD():void
		{
			this._sourceBMD = this._sourceBMD || new BitmapData(this._video.width, this._video.height, true);
			var loc1:*=new Matrix();
			loc1.scale(-1, 1);
			loc1.translate(this._video.width, 0);
			this._sourceBMD.draw(this._video, loc1);
			this._nowPicture = this._nowPicture || new Bitmap();
			this._nowPicture.bitmapData = this._sourceBMD;
			addChild(this._nowPicture);
			return;
		}
		
		private function changeScene(arg1:TimerEvent=null):void
		{
			//this._t600.removeEventListener(TimerEvent.TIMER, this.changeScene);
			//this._t600.reset();
			this.cameraBtn.addEventListener(flash.events.MouseEvent.CLICK, this.beginCountDown);
			this.cameraBtn.mouseEnabled = true;
			this.cameraBtn.alpha = 1;
			removeChild(this._nowPicture);
			//removeChild(this._blackSP);
			this._parent.avatarModel.bmd = this._sourceBMD;
			this._parent.avatarModel.type = 1;
			var loc1:*;
			this._parent.avatarArea.visible = loc1 = true;
			this._parent.localPicArea.visible = loc1;
			this._parent.browseComp.label.visible = true;
			this._parent.splitLines.visible = true;
			this.visible = false;
			this._parent.cancleBtn.visible = loc1 = true;
			this._parent.saveBtn.visible = loc1;
			this._parent.cameraBtn.mouseEnabled = true;
			return;
		}
		
		private function init(arg1:flash.display.DisplayObject):void
		{
			this._parent = arg1 as CutView;
			this._avatar = this._parent.avatarArea;
			this.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onStage);
			this._videoBg = new RectBox(300, 300);
			addChild(this._videoBg);
			if (Camera.names.length) 
			{
				this._camera = Camera.getCamera();
				this._video = new Video(300, 300);
				this._video.smoothing = true;
				this._video.x = 1 + this._video.width;
				this._video.y = 1;
				this._video.scaleX = -1;
				if (this._camera != null) 
				{
					this.hasCamera = true;
					this._camera.setMode(300, 300, 30, false);
					this._camera.setQuality(0, 100);
					this._camera.addEventListener(flash.events.StatusEvent.STATUS, this.cameraStatusHandler);
					this._video.attachCamera(this._camera);
					this.addViewElements();
					this._t = new Timer(100);
					this._t.addEventListener(TimerEvent.TIMER, this.checkCamera);
					this._t.start();
				}
			}
			else 
			{
				this.showTip();
			}
			return;
		}
		
		private function onStage(arg1:flash.events.Event):void
		{
			this.removeEventListener(flash.events.Event.ADDED_TO_STAGE, this.onStage);
			return;
		}
		
		private function showTip():void
		{
			if (this._tip == null) 
			{
				this._tip = new TipB();
				var loc1:*;
				this._tip.y = loc1 = 1;
				this._tip.x = loc1;
			}
			addChild(this._tip);
			return;
		}
		
		private function removeTip():void
		{
			if (!(this._tip == null) && this.contains(this._tip)) 
			{
				removeChild(this._tip);
			}
			return;
		}
		
		private function cameraStatusHandler(arg1:flash.events.StatusEvent):void
		{
			if (arg1.code != "Camera.Muted") 
			{
				this.removeTip();
				this._t.start();
			}
			else 
			{
				addChild(this._videoBg);
				this.showTip();
				this._t.reset();
				this.cameraBtn.visible = false;
			}
			return;
		}
		
		private function checkCamera(arg1:TimerEvent):void
		{
			if (this._camera.currentFPS > 0) 
			{
				this.removeTip();
				this.cameraBtn.visible = true;
			}
			else 
			{
				if (this._delayTime >= 30) 
				{
					this.showTip();
				}
				var loc1:*;
				var loc2:*=((loc1 = this)._delayTime + 1);
				loc1._delayTime = loc2;
				this.cameraBtn.visible = false;
			}
			return;
		}
		
		private function addViewElements():void
		{
			this.cameraBtn = new Button();
			this.cameraBtn.label = "照相";
			this.cameraBtn.width = 80;
			this.cameraBtn.x = 96;
			this.cameraBtn.y = 345;
			this.cameraBtn.addEventListener(flash.events.MouseEvent.CLICK, this.beginCountDown);
			addChildAt(this._videoBg, 0);
			addChildAt(this._video, 1);
			addChild(this.cameraBtn);
			return;
		}
		
		private function newSecurityPanel(arg1:flash.events.MouseEvent):void
		{
			Security.showSettings(SecurityPanel.PRIVACY);
		}
		
		private function beginCountDown(arg1:flash.events.MouseEvent):void
		{
			this.cameraBtn.removeEventListener(flash.events.MouseEvent.CLICK, this.beginCountDown);
			this.cameraBtn.mouseEnabled = false;
			this.cameraBtn.alpha = 0.5;
			this.getAvatarBMD();
			this.changeScene();
		}
		
		private function changeCountDown(arg1:TimerEvent):void
		{
			var loc1:*;
			var loc2:*=((loc1 = this)._currentFrame + 1);
			loc1._currentFrame = loc2;
			//this._countDown.gotoAndStop(this._currentFrame);
			return;
		}
		
		private function showFrameOne(arg1:TimerEvent):void
		{
			//this._countDown.gotoAndStop(this._currentFrame);
			this._tTwo.removeEventListener(TimerEvent.TIMER, this.changeCountDown);
			this._tTwo.removeEventListener(TimerEvent.TIMER_COMPLETE, this.showFrameOne);
			this._tOne = new Timer(1000);
			this._tOne.addEventListener(TimerEvent.TIMER, this.showBlack);
			this._tOne.reset();
			this._tOne.start();
			return;
		}
		
		private function showBlack(arg1:TimerEvent):void
		{
			this._tOne.removeEventListener(TimerEvent.TIMER, this.showBlack);
			if (this._blackSP == null) 
			{
				this._blackSP = new flash.display.Shape();
				this._blackSP.graphics.beginFill(2236962);
				this._blackSP.graphics.drawRect(1, 1, 300, 300);
				this._blackSP.graphics.endFill();
			}
			addChild(this._blackSP);
			this._t100 = this._t100 || new Timer(100);
			this._t100.addEventListener(TimerEvent.TIMER, this.showNowScene);
			this._t100.start();
			return;
		}
		
		private var _parent:CutView;
		
		private var _videoBg:RectBox;
		
		private var _video:Video;
		
		private var _camera:Camera;
		
		private var _avatar:AvatarArea;
		
		private var _nowBMP:Bitmap;
		
		private var _t:Timer;
		
		private var _tTwo:Timer;
		
		private var _tOne:Timer;
		
		private var _t100:Timer;
		
		private var _t600:Timer;
		
		private var _tip:TipB;
		
		private var _delayTime:int=0;
		
		private var _sourceBMD:BitmapData;
		
		private var _currentFrame:int=1;
		
		private var _blackSP:flash.display.Shape;
		
		public var hasCamera:Boolean=false;
		
		public var cameraBtn:Button;
		
		private var _nowPicture:Bitmap;	
	}
}